
package com.cosc645.ecash;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class MoneyOrder implements Serializable {

    private static final long serialVersionUID = 7526472295622776147L;
    public static final int MAX = Integer.MAX_VALUE;
    public static final int MIN = 0;
    public static List<Integer> usedNumbers = new ArrayList<Integer>();
    private double amount;
    private String L0;
    private String R0;
    private int priority;

    public MoneyOrder(double amt) {
        amount = amt;
        Random random = new Random(System.currentTimeMillis());
        int randomNumber = random.nextInt(MAX - MIN) + MIN;
        while (usedNumbers.contains(randomNumber)) {
            randomNumber = random.nextInt(MAX - MIN) + MIN;
        }
        //22 is a generator of 23
        //assign random priority to the money order
        //so they can be popped off in random order
        usedNumbers.add((randomNumber));
        setPriority((randomNumber));
    }

    /**
     * Takes the string in and recreates the object.
     *
     * @param decoded
     */
    public MoneyOrder(String decoded) {

        decoded = decoded.substring(decoded.indexOf("{") + 1);
        decoded = decoded.substring(0, decoded.indexOf("}"));

        String[] values = decoded.split(", ");
        for (String value : values) {
            value = value.replaceAll("[\'\"]", "");
            String[] keyPair = value.split(":");
            try {
                Object fieldValue = keyPair[1];
                Field field = this.getClass().getDeclaredField(keyPair[0]);
                field.setAccessible(true);
                if (field.getType().toString().contains("double")) {
                    fieldValue = new Double(keyPair[1]);
                } else if (field.getType().toString().contains("int")) {
                    fieldValue = new Integer(keyPair[1]);
                }
                field.set(this, fieldValue);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
//        System.out.println("This object is now:" + this.toString());

    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amt) {
        amount = amt;
    }

    public void addCommitment(String left, String right) {
        L0 = left;
        R0 = right;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public void setL0(String l) {
        L0 = l;
    }

    public void setR0(String r) {
        R0 = r;
    }

    public String getL0() {
        return L0;
    }

    public String getR0() {
        return R0;
    }

    @Override
    public String toString() {
        return "MoneyOrder{" +
                "amount:" + amount +
                ", L0:'" + L0 + '\'' +
                ", R0:'" + R0 + '\'' +
                ", priority:" + priority +
                '}';
    }
}
